<template>
  <div id="order">
   <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>订单管理</el-breadcrumb-item>
        <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
        <el-row :gutter="10">
            <el-col :span="8">
                <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getOrders" >
                    <el-button slot="append"  @click="getOrders" icon="el-icon-search"></el-button>
                </el-input>
            </el-col>
        </el-row>

        <el-table :data="orders" border>
            <el-table-column type="index" label="序号" width="50"> </el-table-column>
            <el-table-column prop="order_number" label="订单编号" > </el-table-column>
            <el-table-column width="100" prop="order_price" label="价格" > </el-table-column>
            <el-table-column width="100" prop="order_pay" label="是否支付"> 
                <template slot-scope="scope">
                    <el-tag type="danger" v-show="scope.row.pay_status=='0'">未付款</el-tag>
                    <el-tag type="success" v-show="scope.row.pay_status=='1'">已付款</el-tag>
                </template>
            </el-table-column>
            <el-table-column width="100" prop="is_send" label="是否发货" > </el-table-column>
            <el-table-column prop="create_time" label="订单时间" > 
                <template slot-scope="scope">
                    {{scope.row.create_time|dataFormat}}
                </template>
            </el-table-column>
            <el-table-column  label="操作" > 
                <template slot-scope="scope">
                    <el-button type="primary" @click="showAddressDialog" size="small">收货地址</el-button>
                    <el-button type="success" @click="showEmsDialog()" size="small">物流进度</el-button>
                </template>
            </el-table-column>
        </el-table>

        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 20]" :page-size="queryInfo.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total">
        </el-pagination> 
    </el-card>

    <!-- 对话框 -->
    <el-dialog title="修改收货地址" :visible.sync="addressDialog.addressDialogVisable" width="50%" @close="addressDialogClose">
        <!-- 表单 -->
        <el-form ref="addressFormRef" :rules="addressFormRules" :model="address" label-width="100px">
            <el-form-item label="省市县/区" prop="addressOne">
                <el-cascader :options="cityData" v-model="address.addressOne"></el-cascader>
            </el-form-item>
            <el-form-item label="详细地址" prop="addressTwo">
                <el-input v-model="address.addressTwo"></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="addressDialog.addressDialogVisable=false">取 消</el-button>
            <el-button type="primary" @click="editAddress()">确 定</el-button>
        </span>
    </el-dialog>

    <el-dialog title="查看物流进度" :visible.sync="emsDialogVisable" width="50%" @close="emsDialogClose">
       <el-timeline>
        <el-timeline-item
        v-for="(activity, index) in emsInfo"
        :key="index"
        :timestamp="activity.time">
            {{activity.context}}
        </el-timeline-item>
        </el-timeline>
       
    </el-dialog>
  </div>
</template>

<script>
import cityData from './citydata'
export default {
  name: 'order',
  data() {
      return {
        queryInfo:{
            query:'',
            pagenum	:1,
            pagesize:5
        },
        total:0,
        orders:[],
        addressDialog:{
            addressDialogVisable:false,
            adderss:''
        },
        address:{
            addressOne:[],addressTwo:''
        },
        addressFormRules:{
           addressOne:[{required:true,message:'请选择区域',trigger:'blur'}],
           addressTwo:[{required:true,message:'请输入详细地址',trigger:'blur'}], 
        },
        cityData:cityData,
        emsDialogVisable:false,
        emsInfo:[]
      }
  },
  created(){
      this.getOrders()
  },
  methods:{
      async getOrders(){
          const {data:res}=await this.$http.get('orders',{params:this.queryInfo})
          if(res.meta.status!=200){
              return this.$message.error(res.meta.msg)
          }
          this.total=res.data.total
          this.orders=res.data.goods
      },
      // pagesize改事件
        handleSizeChange(newsize){
            this.queryInfo.pagesize=newsize
            this.getOrders()
        },
        // 页码值发生改变的事件
        handleCurrentChange(newpage){
            this.queryInfo.pagenum=newpage
            this.getOrders()
        },
        showAddressDialog(){
            this.addressDialog.addressDialogVisable=true
        },
        addressDialogClose(){
            this.$refs.addressFormRef.resetFields()
        },
        async showEmsDialog(){
            const{data:res}=await this.$http.get('/kuaidi/1106975712662')
            if(res.meta.status!=200){
              return this.$message.error(res.meta.msg)
            }
            this.emsInfo=res.data
            this.emsDialogVisable=true
        },
        emsDialogClose(){
            this.emsDialogVisable=false
        }
  }
}
</script>

<style scoped>
    .el-cascader{
        width:100%;
    }
</style>
